<!doctype html>
<html>

<head>
    <meta charset="UTF-8">
    <title></title>
    <meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
    <link href="mui.min.css" rel="stylesheet" />
    <style>
        header{
            background: #299CEF;color: #ffffff;text-align: center;
        }
        header h1{
            margin: 0;
        }
        div{
            margin-top: 15px;text-indent: 20px;
        }
        button{
            width: 100%;
        }
        #success{
            margin-top:55px;
        }
        #error{
            margin-top:55px;
            margin-bottom: 55px;
        }
        #goon{
            outline: none;border:0;color:white;
        }
    </style>
</head>
<body onbeforeunload="history.go(0)">
<header class="mui-bar mui-bar-nav topColor" style="background:#299CEF">
    <h1 class="mui-title">一卡通充值结果查询</h1>
</header>
<div id="fail" style="display:none">充值失败</div>
<div id="success" style="display: none">
    <div id="result"></div>
    <div>卡号：<span id="id"></span></div>
    <div>充值：<span id="money"></span></div>
    <div id="orderId">订单号：</div>
    <div class="time"></div>
</div>
<div id="error" style="text-align: center">充值正在入账，请勿关闭此页面...</div>
<!--<button type="button" class="mui-btn mui-btn-primary" id="record">查看充值记录</button>-->
<button type="button" class="mui-btn mui-btn-primary btn-blue" id="goon" style="background:#299CEF" disabled="disabled">返回</button>
<script src="mui.min.js"></script>

<script type="text/javascript">
    mui.init();
    mui.plusReady(function () {
        var w = plus.webview.currentWebview();
        document.getElementById('goon').onclick = function () {
            w.close();
        };
        plus.key.addEventListener('backbutton',function () {
            w.close();
        });
        var search = window.location.href;
        var firstChange = decodeURI(search.split('?')[1].split('&')[0].split('=')[1].replace(/%3A/gi,':'));
        var secondChange = firstChange.replace(/%2c/gi,',');
        var Json = JSON.parse(secondChange)
        var trandeId = Json.transid;
        mui.ajax({
            url:'/cwp/front/sh/pay!execute?uid=P016&tradeId='+trandeId,
            type:'post',
            success:function (res) {
                console.log(JSON.stringify(res))
                var isCharged,lastCardLogOrderNo;
                if(res.returnCode == 0) {
                    isCharged = res.returnMessage;
                    //获取下一个充值记录流水号
                    mui.ajax({
                        url:'/cwp/front/sh/pay!execute?uid=P017',
                        type:'post',
                        success:function (json) {
                            console.log(JSON.stringify(json))
                            if(json.returnCode == 0) {
                                lastCardLogOrderNo = json.returnMessage;
                                //获取支付结果
                                mui.ajax({
                                    url:'/cwp/front/sh/pay!execute?uid=P011&tradeId='+Json.cporderid,
                                    data:{
                                        isCharged:isCharged,
                                        lastCardLogOrderNo:lastCardLogOrderNo
                                    },
                                    type:'post',
                                    success:function (data) {
                                        if(res.returnCode == '0'){
                                            //保存充值记录
                                            console.log("充值保存记录");
                                            console.log(data.object);
                                            mui.ajax({
                                                url:'/cwp/front/sh/pay!execute?uid=P018',
                                                type:'post',
                                                data:{
                                                    resultJson : JSON.stringify(data.object)
                                                },
                                                success:function (response) {
                                                    document.getElementById('success').style.display = 'block';
                                                    document.getElementById('error').style.display = 'none';
                                                    document.getElementById('id').appendChild(document.createTextNode(Json.cpprivate.split(',')[0]))
                                                    document.getElementById('money').innerHTML = Json.money;
                                                    document.getElementById('orderId').appendChild(document.createTextNode(Json.cporderid));
                                                    document.getElementById('goon').removeAttribute('disabled');
                                                }
                                            });
                                            document.getElementById('result').appendChild(document.createTextNode('充值成功'));

                                        }else{
                                            document.getElementById('result').appendChild(document.createTextNode('充值失败'));
                                        }
                                    }
                                })
                            }else{
                                document.getElementById('fail').style.display = 'block';
                                document.getElementById('error').style.display = 'none';
                            }
                        }
                    })
                }
            }
        })
    })
</script>
</body>

</html>